package co.edu.usbcali.sam.presentation.backingBeans;

import java.io.Serializable;
import java.util.List;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import co.edu.usbcali.sam.exceptions.ZMessManager;
import co.edu.usbcali.sam.modelo.MatriculaAcademica;
import co.edu.usbcali.sam.modelo.dto.MatriculaAcademicaDTO;
import co.edu.usbcali.sam.presentation.businessDelegate.IBusinessDelegatorView;
import co.edu.usbcali.sam.utilities.FacesUtils;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * www.zathuracode.org
 *
 */
@ManagedBean
@ViewScoped
public class MatriculaAcademicaView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtAnoLectivo;
    private InputText txtIdEstudiante_Estudiantes;
    private InputText txtIdMatAca;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<MatriculaAcademicaDTO> data;
    private MatriculaAcademicaDTO selectedMatriculaAcademica;
    private MatriculaAcademica entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;

    public MatriculaAcademicaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            MatriculaAcademicaDTO matriculaAcademicaDTO = (MatriculaAcademicaDTO) e.getObject();

            if (txtAnoLectivo == null) {
                txtAnoLectivo = new InputText();
            }

            txtAnoLectivo.setValue(matriculaAcademicaDTO.getAnoLectivo());

            if (txtIdEstudiante_Estudiantes == null) {
                txtIdEstudiante_Estudiantes = new InputText();
            }

            txtIdEstudiante_Estudiantes.setValue(matriculaAcademicaDTO.getIdEstudiante_Estudiantes());

            if (txtIdMatAca == null) {
                txtIdMatAca = new InputText();
            }

            txtIdMatAca.setValue(matriculaAcademicaDTO.getIdMatAca());

            Long idMatAca = FacesUtils.checkLong(txtIdMatAca);
            entity = businessDelegatorView.getMatriculaAcademica(idMatAca);

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedMatriculaAcademica = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedMatriculaAcademica = null;

        if (txtAnoLectivo != null) {
            txtAnoLectivo.setValue(null);
            txtAnoLectivo.setDisabled(true);
        }

        if (txtIdEstudiante_Estudiantes != null) {
            txtIdEstudiante_Estudiantes.setValue(null);
            txtIdEstudiante_Estudiantes.setDisabled(true);
        }

        if (txtIdMatAca != null) {
            txtIdMatAca.setValue(null);
            txtIdMatAca.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtId() {
        try {
            Long idMatAca = FacesUtils.checkLong(txtIdMatAca);
            entity = (idMatAca != null)
                ? businessDelegatorView.getMatriculaAcademica(idMatAca) : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtAnoLectivo.setDisabled(false);
            txtIdEstudiante_Estudiantes.setDisabled(false);
            txtIdMatAca.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtAnoLectivo.setValue(entity.getAnoLectivo());
            txtAnoLectivo.setDisabled(false);
            txtIdEstudiante_Estudiantes.setValue(entity.getEstudiantes()
                                                       .getIdEstudiante());
            txtIdEstudiante_Estudiantes.setDisabled(false);
            txtIdMatAca.setValue(entity.getIdMatAca());
            txtIdMatAca.setDisabled(true);
            btnSave.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedMatriculaAcademica = (MatriculaAcademicaDTO) (evt.getComponent()
                                                                 .getAttributes()
                                                                 .get("selectedMatriculaAcademica"));
        txtAnoLectivo.setValue(selectedMatriculaAcademica.getAnoLectivo());
        txtAnoLectivo.setDisabled(false);
        txtIdEstudiante_Estudiantes.setValue(selectedMatriculaAcademica.getIdEstudiante_Estudiantes());
        txtIdEstudiante_Estudiantes.setDisabled(false);
        txtIdMatAca.setValue(selectedMatriculaAcademica.getIdMatAca());
        txtIdMatAca.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedMatriculaAcademica == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new MatriculaAcademica();

            Long idMatAca = FacesUtils.checkLong(txtIdMatAca);

            entity.setAnoLectivo(FacesUtils.checkString(txtAnoLectivo));
            entity.setIdMatAca(idMatAca);
            entity.setEstudiantes((FacesUtils.checkLong(
                    txtIdEstudiante_Estudiantes) != null)
                ? businessDelegatorView.getEstudiantes(FacesUtils.checkLong(
                        txtIdEstudiante_Estudiantes)) : null);
            businessDelegatorView.saveMatriculaAcademica(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Long idMatAca = new Long(selectedMatriculaAcademica.getIdMatAca());
                entity = businessDelegatorView.getMatriculaAcademica(idMatAca);
            }

            entity.setAnoLectivo(FacesUtils.checkString(txtAnoLectivo));
            entity.setEstudiantes((FacesUtils.checkLong(
                    txtIdEstudiante_Estudiantes) != null)
                ? businessDelegatorView.getEstudiantes(FacesUtils.checkLong(
                        txtIdEstudiante_Estudiantes)) : null);
            businessDelegatorView.updateMatriculaAcademica(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedMatriculaAcademica = (MatriculaAcademicaDTO) (evt.getComponent()
                                                                     .getAttributes()
                                                                     .get("selectedMatriculaAcademica"));

            Long idMatAca = new Long(selectedMatriculaAcademica.getIdMatAca());
            entity = businessDelegatorView.getMatriculaAcademica(idMatAca);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Long idMatAca = FacesUtils.checkLong(txtIdMatAca);
            entity = businessDelegatorView.getMatriculaAcademica(idMatAca);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteMatriculaAcademica(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedMatriculaAcademica = (MatriculaAcademicaDTO) (evt.getComponent()
                                                                     .getAttributes()
                                                                     .get("selectedMatriculaAcademica"));

            Long idMatAca = new Long(selectedMatriculaAcademica.getIdMatAca());
            entity = businessDelegatorView.getMatriculaAcademica(idMatAca);
            businessDelegatorView.deleteMatriculaAcademica(entity);
            data.remove(selectedMatriculaAcademica);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String anoLectivo, Long idMatAca,
        Long idEstudiante_Estudiantes) throws Exception {
        try {
            entity.setAnoLectivo(FacesUtils.checkString(anoLectivo));
            businessDelegatorView.updateMatriculaAcademica(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("MatriculaAcademicaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtAnoLectivo() {
        return txtAnoLectivo;
    }

    public void setTxtAnoLectivo(InputText txtAnoLectivo) {
        this.txtAnoLectivo = txtAnoLectivo;
    }

    public InputText getTxtIdEstudiante_Estudiantes() {
        return txtIdEstudiante_Estudiantes;
    }

    public void setTxtIdEstudiante_Estudiantes(
        InputText txtIdEstudiante_Estudiantes) {
        this.txtIdEstudiante_Estudiantes = txtIdEstudiante_Estudiantes;
    }

    public InputText getTxtIdMatAca() {
        return txtIdMatAca;
    }

    public void setTxtIdMatAca(InputText txtIdMatAca) {
        this.txtIdMatAca = txtIdMatAca;
    }

    public List<MatriculaAcademicaDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataMatriculaAcademica();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<MatriculaAcademicaDTO> matriculaAcademicaDTO) {
        this.data = matriculaAcademicaDTO;
    }

    public MatriculaAcademicaDTO getSelectedMatriculaAcademica() {
        return selectedMatriculaAcademica;
    }

    public void setSelectedMatriculaAcademica(
        MatriculaAcademicaDTO matriculaAcademica) {
        this.selectedMatriculaAcademica = matriculaAcademica;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}
